package com.authine.cloudpivot.ext.controller;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.vo.oa.OARequest;
import com.authine.cloudpivot.ext.vo.oa.ProcessMaterielsVo;
import com.authine.cloudpivot.ext.vo.oa.StartProcessVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/toOa")
@Slf4j
public class OaController {


    @Autowired RestTemplate restTemplate;

    @RequestMapping("/startProcess")
    public Object startProcess(){

        log.info("》》》》》》》进入oa control");

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://172.16.228.212:8094/oa/process/new";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("ClientId","com.primeton.esb.consumer.nc");
        headers.set("OperationCode","com.primeton.esb.producer.oa.submission.process");
        headers.set("Timestamp", String.valueOf(System.currentTimeMillis()));
        //Authorization
        String plainCreds = "administrator:P@ssword01";
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);
        headers.add("Authorization", "Basic " + base64Creds);

        OARequest<StartProcessVo> oa = new OARequest<>();

        oa.setType("359");
        oa.setDescrition("到货验收流程");
        List<StartProcessVo> listSpv = new ArrayList<>();
        StartProcessVo spv = new StartProcessVo();
        listSpv.add(spv);
        oa.setData(listSpv);

        spv.setApply_date("2020-08-14");
        spv.setOrder_number("PO-202002161");
        spv.setSupplier("上海司南卫星导航技术股份有限公司");
        spv.setDelivery_number("CR2020081400001555");
        spv.setWarehouse("委外库");
        spv.setMakesure_people("v-haibo.li");
        spv.setActive_type( "2");
        spv.setAccept_sum("4.00000000");

        List<ProcessMaterielsVo> listPmv = new ArrayList<>();
        ProcessMaterielsVo pmv = new ProcessMaterielsVo();
        listPmv.add(pmv);
        spv.setMateriels(listPmv);

        pmv.setPurch_type("208");
        pmv.setSpec("SR2");
        pmv.setMaterialcode( "46002010002");
        pmv.setMaterialname("棒棒糖");
        pmv.setOrder_num(4);
        pmv.setAccept_num(4);
        pmv.setCoin_type("人民币");
        pmv.setNtaxrate(13);
        pmv.setPurch_price(6700);
        pmv.setPurch_price_notax( 5929.2035d);
        pmv.setAccept_makesure(4);
        pmv.setAccept_date("2020-08-14");
        pmv.setUnqualified("0");
        pmv.setTax_amount(26800);
        pmv.setCgeneralbid("1001XX100000001D4TP9");




//        MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
//        map.add("email", "844072586@qq.com");

        HttpEntity<OARequest<StartProcessVo>> request = new HttpEntity<>(oa, headers);

        ResponseEntity<JSONObject> response = restTemplate.postForEntity( url, request , JSONObject.class );

        log.info("》》》》》调用oa 接口返回结果：\n");
        System.out.println(response.getBody());


        return  null;
    }


}
